// dependencies: superagent, cheerio

var express = require("express");
var superagent = require("superagent");
var cheerio = require("cheerio");

var app = express();

var url = "http://dig.chouti.com/";

app.get("/", function(req, res, next){

    superagent.get(url).end(function(err, sres){
        // next() ????
        if(err){
            return next(err);
        }

        var $ = cheerio.load(sres.text);
        var items = [];
        $("#content-list .news-content").each(function(index, elem){
            var $elem = $(elem);
            var part1 = $elem.children(".part1");
            var part2 = $elem.children(".part2");

            items.push({
                href: part1.children(".show-content").attr("href"),
                title: part2.attr("share-title"),
                img: part2.attr("share-pic")
            });
            // items.push({
            //     title : $elem.attr("share-title"),
            //     // href : $elem.attr("href"),
            //     img : $elem.attr("share-pic")
            // });
        });

        res.send(items);

    });
});

app.listen(3000, function(){
    console.log("Server listen at http://localhost:3000");
});